package com.starblink.wishlist.createboard.ui

import com.alibaba.android.arouter.facade.annotation.Route
import com.starblink.basic.route.RoutePage
import com.starblink.android.basic.base.activity.BaseTVMActivity
import android.os.Bundle
import android.view.inputmethod.EditorInfo
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.starblink.android.basic.bean.search.SearchResultPageArgs
import com.starblink.android.basic.ext.click
import com.starblink.android.basic.ext.string
import com.starblink.android.basic.ext.toast
import com.starblink.android.basic.util.bus.FlowBus
import com.starblink.android.basic.util.bus.FlowConst
import com.starblink.basic.style.title.OnTitleBarListener
import com.starblink.basic.style.title.TitleBar
import com.starblink.library.widget.R
import com.starblink.library.widget.wishlist.bean.BatchProductArrEntry
import com.starblink.wishlist.databinding.ActivityCreateBoardBinding
import kotlinx.coroutines.coroutineScope

@Route(path = RoutePage.Wishlist.CREATE_BOARD)
class CreateBoardActivity : BaseTVMActivity<ActivityCreateBoardBinding, CreateBoardVM>() {

    @JvmField
    @Autowired(name = RoutePage.ARG.BATCH_PRODUCT)
    var batchPros: ArrayList<BatchProductArrEntry>? = null

    @JvmField
    @Autowired(name = RoutePage.ARG.IS_WISH_LIST)
    var isWishlist: Boolean = false

    @JvmField
    @Autowired(name = RoutePage.ARG.WISH_ID)
    var fromWishListId: String? = null


    override fun initViewBinding(): ActivityCreateBoardBinding =
        ActivityCreateBoardBinding.inflate(layoutInflater)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        viewBinding.titleBar.setOnTitleBarListener(object : OnTitleBarListener {
            override fun onLeftClick(titleBar: TitleBar?) {
                finish()
            }
        })

        viewBinding.boardTitle.setOnEditInputListener {
            viewBinding.btnCreate.isEnabled = it.isNotEmpty()
        }
        viewBinding.boardTitle.editText.imeOptions = EditorInfo.IME_ACTION_NEXT

        viewBinding.btnCreate.isEnabled =
            viewBinding.boardTitle.text == null

        viewBinding.boardDesc.editText.imeOptions = EditorInfo.IME_ACTION_DONE

        viewBinding.btnCreate.click {
            viewModel.createBoard(
                isWishListPage = isWishlist,
                batchPros = batchPros,
                title = viewBinding.boardTitle.text,
                desc = viewBinding.boardDesc.text.ifEmpty { null },
                fromWishListId = fromWishListId,
                successCallback = {
                    FlowBus.boolFlowBusNotify(FlowConst.WISHLIST_BOARD_REFRESH)
                    finish()
                })
        }
    }

}